#!../mlisp

;;; conCATnate
;;; print files

;; $0     : execute script name (string)
;; *args* : command line arguments (list)

(load "arc.arc")

(def error msg
  (apply prn msg)
  (quit 1))

(mac with-open-file (parms . body)
  (with (var (car parms)
         fname (cadr parms))
    (w/uniq (gfn gf)
      `(withs (,gfn ,fname
               ,gf (open ,gfn))
         (iflet ,var ,gf
                (do
                  ,@body
                  (close ,var))
            (error "can't open: " ,gfn))))))

(def echo-stream (fs)
  (whilet line (read-line fs)
    (prn line)))

(if *args*
    (each fname *args*
          (with-open-file (fs fname)
            (echo-stream fs)))
  (echo-stream *standard-input*))
